package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("add_IPBankService")
public class Add_IPBankService {
    ResultSet rs =null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public boolean add(String content) throws SQLException {
        JSONArray arr = JSONArray.fromObject(content);
        boolean flag = false;

        if (!arr.isEmpty()) {
            for (int i = 0; i < arr.size(); i++) {
                JSONObject one = arr.getJSONObject(i);
                String ipType = one.getString("IPType");
                String areaName = one.getString("areaName");
                String startIP = one.getString("startIP");
                String endIP = one.getString("endIP");
                String sql = "select * from iprangeinfo as w where type = '" + ipType + "'"
                        + " and areaName='" + areaName + "'"
                        + " and startIP ='" + startIP + "'"
                        + " and endIP='" + endIP + "'";
//                List hfdws = session.createQuery(hql).list();
                Object[] params = {};
                rs = baseDao.executeSQL(sql, params);
                //如果新的当前行有效，则返回 true；如果不存在下一行，则返回 false
                if (!rs.next()) {
                    Object[] param = {areaName, startIP, endIP,ipType};
                    sql = "insert into appdef (areaName,startIP, endIP,type) values (?, ?, ?)";
                    baseDao.executeUpdate(sql, param);
                }

            }


//        JSONArray arr = JSONArray.fromObject(content);,
//        boolean flag = false;
//        GeneralFileProcess pro = new GeneralFileProcess();
//        if (!arr.isEmpty()) {
//            pro.init();
//            Map<String, Integer> map = pro.getareids();
//            for (int i = 0; i < arr.size(); i++) {
//                JSONObject one = arr.getJSONObject(i);
//                String ipType = one.getString("IPType");
//                String areaName = one.getString("areaName");
//                String startIP = one.getString("startIP");
//                String endIP = one.getString("endIP");
//                int areaNameId = map.size() + 1;
//                if (map.containsKey(areaName)) {
//                    areaNameId = map.get(areaName);
//                } else {
//                    map.put(areaName, areaNameId);
//                }
//                pro.insert(GeneralFileProcess.INSERT_IP_BANK, new Object[]{areaName, startIP, endIP, ipType, areaNameId});
//            }
            flag = true;
        }
        close();
//        pro.close();
        return flag;
    }
}
